
// 判断 SSOP后台配置下单人数，加入购物车时做限制
// 购买限制 type: 1 无限制， 2 有限制：less 最少购买数量，more 最多购买数量
export function addCartLimit (data = {}){
    let minNum = 1, maxNum = 99, isLimit = false;
    const { stock, predefinedRuleAppDTO, orderRules } = data,
          { orderNumberType, orderNumberLess, orderNumberMore } = predefinedRuleAppDTO || orderRules || {};
    if(orderNumberType === 2){
        minNum = orderNumberLess || minNum;
        maxNum = orderNumberMore || maxNum;
    }
    // 库存比最小值小，不能加入购物车
    if(stock !== -1 && stock < minNum){
        isLimit = true
    }
    return {
        isLimit,
        minNum,
        maxNum
    }
}


// 处理活动券的数据格式，同优惠券
export function formatCouponData (data){
    let name, rebatePrice, discountPrice;
    // 优惠券
    if(data.couponId){
        discountPrice = data.rebateMethod == 2 ? `${(data.rebatePrice)/(data.rebatePrice>=10 ? 10 : 1)}折` : "";
        return {
            ...data,
            discountPrice,
        }
    }
     // 活动券
    if(data.saleRole){
        const { couponName, saleType, saleDiscountOrMoney } = data.saleRole;
        name = couponName;
        rebatePrice = saleDiscountOrMoney;
        discountPrice = saleType == 2 ? `${(saleDiscountOrMoney)/(saleDiscountOrMoney>=10 ? 10 : 1)}折` : "";
    }else if(data.minusRule){
        name = data.minusRule.minusRuleAlgorithmList[0].couponName;
        rebatePrice = data.minusRule.minusRuleAlgorithmList[0].salePrice;
    }else if(data.reservationRole){
        name = data.reservationRole[0].couponName;
        rebatePrice = data.reservationRole[0].salePrice;
    }
    return {
        ...data,
        name,
        rebatePrice,
        discountPrice
    }
}
